//
//  WordOrderRequest.swift
//  Employee
//
//  Created by swordjoy on 2017/4/5.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON
import Moya

struct WorkorderRequest: DataResultProtocol {
  
  static let shared = WorkorderRequest()
  
  
  // MARK: - 获取首页布局
  
  func getHomeLayout(success: @escaping (HomeLayoutModel) -> () ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getHomeLayout()) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let model = HomeLayoutModel(json: data)
        success(model)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getHomeLayout(success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 获取分类列表
  
  func getWorkOrderCategory(success: @escaping (SJTree) -> () ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getWorkOrderCategory()) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let tree = SJTree(json: data)
        success(tree)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getWorkOrderCategory(success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
  
  }
  
  
  // MARK: - 获取组织架构(新建工单)
  
  func getOrganization(success: @escaping ([Department]) -> () ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getOrganization()) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let departments = Department.array(from: data.arrayValue)
        success(departments)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getOrganization(success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
  
  }
  
  // MARK: - 新建工单
  
  func newOrder(by model: NewOrderModel, success: @escaping VoidClosure) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.newOrder(model: model)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        success()
      
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.newOrder(by: model, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  
  // MARK: - 获取工单列表by类别
  
  func getOrder(status: String, pagepn: String, success: @escaping ([WorkOrder], PageModel) -> Void, failure: @escaping VoidClosure) -> Cancellable? {
  
    let requestKey = SJApp.functionKey() + status
    if let cacheCancle = GlobalConfig.requestCache[requestKey] {
      return cacheCancle
    }
    
    let cancle =  WorkOrderProvider.request(WorkOrderAPI.getOrder(status: status, pagepn: pagepn)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let workOrders = WorkOrder.array(from: data["elements"].arrayValue)
        let pageModel = PageModel(json: data)
        success(workOrders, pageModel)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getOrder(status: status, pagepn: pagepn, success: success, failure: failure)
        
      }, failure: failure)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  func getTimeOutOrder(pagepn: String, success: @escaping ([WorkOrder], PageModel) -> Void) -> Cancellable? {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getTimeOutOrder(pagepn: pagepn)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let orders = WorkOrder.array(from: data["elements"].arrayValue)
        
        let page = PageModel(json: data)
        
        success(orders, page)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getTimeOutOrder(pagepn: pagepn, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 获取工单列表(首页)
  
  func getCurrentOrder(success: @escaping ([WorkOrder]) -> Void, failure:  VoidClosure? = nil ) -> Cancellable? {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getCurrentOrder()) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
    
        let workOrders = WorkOrder.array(from: data.arrayValue)
        success(workOrders)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getCurrentOrder(success: success, failure: failure)
        
      }, failure: failure)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 获取巡检列表
  
  func getInspectionPlans(date: String, success: @escaping ([Inspection]) -> Void, failure: VoidClosure? = nil) -> Cancellable? {
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getInspectionPlans(date: date)) { (result) in
      
      self.dataProtocol(with: result, success: { (data) in
        
        let inspections = Inspection.array(from: data.arrayValue)
        success(inspections)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getInspectionPlans(date: date, success: success, failure: failure)
        
      }, failure: failure)
      
    }
    
    return cancle
    
  }
  
  // MARK: - 获取工单详情
  
  func getOrderDetail(id: String, success: @escaping (OrderDetailModel) -> Void) -> Cancellable? {
  
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle =  WorkOrderProvider.request(WorkOrderAPI.getOrderDetail(id: id)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let orderDetailModel = OrderDetailModel(json: data)
        success(orderDetailModel)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getOrderDetail(id: id, success: success)
        
      }, failure: { 
        
      })
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 处理工单(转交/完结/回复)
  
  func handleOrder(type: String, workOrderId: String, staffId: String, replyContent: String, images: String, success: @escaping VoidClosure, failure: @escaping VoidClosure) -> Cancellable? {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.handleOrder(type: type, workOrderId: workOrderId, staffId: staffId, replyContent: replyContent, images: images)) { (result) in
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        success()
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.handleOrder(type: type, workOrderId: workOrderId, staffId: staffId, replyContent: replyContent, images: images, success: success, failure: failure)
        
      }, failure: failure)
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 获取巡检详情
  
  func getInspectionDetail(recordId: String, status: String, success: @escaping (InspectionDetailModel) -> Void ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getInspectionDetail(recordId: recordId, status: status)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let model = InspectionDetailModel(json: data)
        success(model)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getInspectionDetail(recordId: recordId, status: status, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
  }
  
  // MARK: - 获取巡检详情 未检查
  
  func getWaitInspectionDetail(recordId: String, success: @escaping (InspectionFormModel) -> Void ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getInspectionDetail(recordId: recordId, status: "WaitInspection")) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let model = InspectionFormModel(json: data)
        success(model)
        
      }, overdue: {let _ = WorkorderRequest.shared.getWaitInspectionDetail(recordId: recordId, success: success)
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
  }
  
  // MARK: - 获取巡检表单by二维码
  
  func getInspectionForm(areaNumber: String, success: @escaping (InspectionFormModel) -> Void ) -> Cancellable {
  
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.getInspectionForm(areaNumber: areaNumber)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        if data.isEmpty {
            SJHint.show("没有该巡检项目")
        } else {
            let model = InspectionFormModel(json: data)
            success(model)
        }
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.getInspectionForm(areaNumber: areaNumber, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
  // MARK: - 提交工单
  
  func submitInspectionForm(areaId: String, resultData: String, success: @escaping VoidClosure) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.submitInspectionForm(areaId: areaId, resultData: resultData)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        success()
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.submitInspectionForm(areaId: areaId, resultData: resultData, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
  
  }
  
  // MARK: - 搜索工单
  
  func searchOrder(search: String, pagepn: String, success: @escaping ([WorkOrder], PageModel) -> Void ) -> Cancellable {
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.searchOrder(search: search, pagepn: pagepn)) { (result) in
      
      self.dataProtocol(with: result, success: { (data) in
        
        let workOrders = WorkOrder.array(from: data["elements"].arrayValue)
        let page = PageModel(json: data)
        
        success(workOrders, page)
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.searchOrder(search: search, pagepn: pagepn, success: success)
        
      }, failure: nil)
      
    }
    
    return cancle
    
  }
  
  func feedback(content: String, success: @escaping VoidClosure) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = WorkOrderProvider.request(WorkOrderAPI.feedback(content: content)) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        success()
        
      }, overdue: { 
        
        let _ = WorkorderRequest.shared.feedback(content: content, success: success)
        
      }, failure: nil)
      
    }
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
  
}
